/**
  * @(#)NamespaceSettings.java
  *
  * Return the manager as configured in the properties files. This
  * requires retrieval of the implementation specified in the primary
  * config and loading the class definition. The implementation
  * settings are loaded into the specified namespace, the instance is
  * initialized and then returned. This method only performs logging
  * for invocations that specify enableLogging = true.
  *
  * @author Course Scheduler Team
  * 
  * @license GNU General Public License version 3 (GPLv3)
  *
  * This file is part of Course Scheduler, an open source, cross platform
  * course scheduling tool, configurable for most universities.
  *
  * Copyright (C) 2010-2012 Course Scheduler Team
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * 
  */
package com.pollicitus.scheduler.core;

import java.io.FileNotFoundException;
import java.io.IOException;


/**
 * Wrap the primary settings object with the proper namespace. Using this interface
 * allows the application components to avoid knowing the specific namespace in
 * which the settings were loaded.  
 * 
 * @author Course Scheduler Team
 *
 */
public class NamespaceSettings extends Settings {

	//private class constants
	private String namespace;
	private String completeNamespace;
	
	/**
	 * Wrap the application settings class in order to simplify access
	 * to the specified namespace's settings. Uses the specified settings
	 * object as its backing store (thus any changes are globally available).
	 * 
	 * @param settings the primary application settings instance
	 */
	public NamespaceSettings(Settings settings, String namespace){
		//subclasses of Settings should use this constructor
		super(settings);
		this.namespace = namespace;
		this.completeNamespace = this.namespace + Settings.NAMESPACE_SEPARATOR;
	}
	
	/* (non-Javadoc)
	 * @see com.scheduler.core.Settings#getProperty(java.lang.String)
	 */
	@Override
	public String getProperty(String propertyName){
		if(propertyName.contains(Settings.NAMESPACE_SEPARATOR)){
			//if the namespace is already included, process normally
			return super.getProperty(propertyName);
		}else
			//otherwise, prepend the complete namespace
			return super.getProperty(completeNamespace + propertyName);
	}
	
	/**
	 * Save this Namespace Settings back to its source properties file. This
	 * should be used when only this particular namespace should be saved to disk.
	 * This uses the Settings.save(namespace) method to flush to disk.
	 * 
	 * @throws IOException if there is a problem writing the settings back
	 * @throws FileNotFoundException  if the config file is not found
	 */
	public void save() throws FileNotFoundException, IOException{
		save(namespace);
	}
}
